package com.yzc.mihuyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzc.mihuyou.entity.*;
import com.yzc.mihuyou.mapper.*;
import com.yzc.mihuyou.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.mihuyou.util.CommentUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 话题表 服务实现类
 * </p>
 *
 * @author 易紫诚
 * @since 2022-04-28
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Autowired
    TopicMapper topicMapper;
    @Autowired
    InvitationMapper invitationMapper;
    @Autowired
    PlateMapper plateMapper;
    @Autowired
    IngvseService ingvseService;
    @Autowired
    UserService userService;
    @Autowired
    GiveMapper giveMapper;
    @Autowired
    TopicService topicService;
    @Autowired
    CommentService commentService;
    @Autowired
    IccompMapper iccompMapper;
    @Autowired
    AttentionService attentionService;

    @Override
    public Topic selTope(int topic_id) {
        return topicMapper.selectById(topic_id);
    }

    @Override
    public List<Topic> selTue(int id,Integer i) {
        System.out.println(i);
        QueryWrapper<Topic> queryWrapper=new QueryWrapper<>();
        Page<Topic> page=new Page();
        if(id>0){
            queryWrapper.eq("arrtu",id);
            queryWrapper.eq("recommend",0);
            if(i==  null && i==0){
                page.setSize(0);
                page.setCurrent(5);
            }
        }else {
            queryWrapper.eq("recommend",0);
            if(i==null && i==0){
                page.setSize(20L);
                page.setCurrent(0);
            }
        }
        return topicMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, Object> selInse(int id, int type, PageWuer pageWuer,Long uid) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper queryWrapper2ro=new QueryWrapper();
        queryWrapper2ro.eq("topic_id",id);
        map.put("Toty",topicMapper.selectOne(queryWrapper2ro    ));
        Page<Invitation> page = new Page<>(new Long(pageWuer.getCurrent()), new Long(pageWuer.getSize()));
        QueryWrapper<Invitation> queryWrapper=new QueryWrapper<>();
        queryWrapper.likeLeft("a.topic_id",","+id);
        queryWrapper.apply("a.invition_id=b.invitation_id");
        if(type==0){
            queryWrapper.orderByDesc("b.gmt_modified");
        }else {
            queryWrapper.orderByDesc("b.gmt_dian");
        }
         List<Invitation> invitationsList=topicMapper.Inle(page,queryWrapper);


        List list = new ArrayList<>(); //获取发帖的用户信息
        for (int i = 0; i < invitationsList.size(); i++){
            Map<String, Object> map1 = new HashMap<>();
//           帖子
            map1.put("nvitations", invitationsList.get(i));
            Document doc = Jsoup.parse(invitationsList.get(i).getGmtContent());
//           如果是首页的话就回去帖子的模块
            if(id==0){
                map1.put("plate",plateMapper.selectById(invitationsList.get(i).getGmtReprint()));
            }
            Elements element = doc.getElementsByTag("p");
            Elements element2 = doc.getElementsByTag("img");
            element2.attr("preview","imgPreview");
            List<String> listImgs=new ArrayList<>();
            for (int op=0;op<element2.size();op++){
                listImgs.add(op,element2.get(op).attr("src"));
            }
            map1.put("img",listImgs);
            map1.put("text",element.text());
//            创建帖子的用户
            map1.put("nvitationsUser", userService.selUser(invitationsList.get(i).getInvitationCome()));
            //获取关注的数量
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("give_userid", invitationsList.get(i).getInvitationId());
            queryWrapper2.eq("invtation_ore",1);
            invitationsList.get(i).setSums(giveMapper.selectCount(queryWrapper2));
//        获取点赞的数量
            QueryWrapper queryWrapper3 = new QueryWrapper();
            queryWrapper2.eq("give_userid", invitationsList.get(i).getInvitationId());
            queryWrapper2.eq("invtation_prin", 1);
            invitationsList.get(i).setZwu(giveMapper.selectCount(queryWrapper3));
//            获取帖子的话题
            Ingvse ingvse= ingvseService.selTope(invitationsList.get(i).getInvitationId());
            if(ingvse.getInvitionId()!=null && !ingvse.getTopicId().equals("")){
                List<String> ingvseList= CommentUtils.strUwe(ingvse.getTopicId());
                List<Topic> list1 = new ArrayList<>();
                for (int ik=0;ik<ingvseList.size();ik++){
                    if(!ingvseList.get(ik).equals("")){
                        list1.add(ik,topicService.selTope(Integer.parseInt(ingvseList.get(ik))));
                    }
                }
                map1.put("tope", list1);
            }else {
                map1.put("tope", null);
            }
//           获取评论数量
                QueryWrapper<Invitation> queryWrappersr = new QueryWrapper<>();
                queryWrapper.eq("giv_we",0);
                queryWrapper.eq("blog_id", invitationsList.get(i).getInvitationId());
                System.out.println(invitationsList.get(i).getInvitationId());
                invitationsList.get(i).setCommentNum(invitationMapper.rowesNun(queryWrappersr).getCommentNum());
//          是否收藏 是否点赞
            if (uid > 0) {
                QueryWrapper<Give> queryWrappery = new QueryWrapper<>();
                queryWrappery
                        .eq("invitation_id", invitationsList.get(i).getInvitationId())
                        .eq("give_userid", uid);
                Give give = giveMapper.selectOne(queryWrappery);
                map1.put("igact", give);
            }
            list.add(i, map1);
        }
        map.put("InveArr", list);
        PageWuer pages=new PageWuer(page.getTotal(),page.getSize(),page.getCurrent(),page.getPages());
        map.put("sizeArr", pages);

        return map;
    }

    @Override
    public List<Topic> listTopes(String str) {
        QueryWrapper<Topic> topicQueryWrapper=new QueryWrapper<>();
        System.out.println(str);
        topicQueryWrapper.like("topic_come",str);
        return topicMapper.selectList(topicQueryWrapper);
    }
}
